package kayao.client.scene.objects;


import android.util.FloatMath;

import com.chopsticks3d.math.FastMath;
import com.chopsticks3d.scene.camera.Camera;

/**
* A Camera following a car in a third person perspective.
*/
public class CarCamera extends Camera {	
	private static float POSITION_Y = 3.0f;
	private static float RADIUS = 3.0f;
	
	private CarNode mCar;
	private float mAngle;
	private float mX;
	private float mZ;
	
	/**
	 * Constructor which sets the car to follow.
	 * @param car the car.
	 */
	public CarCamera(CarNode car) {
		mCar = car;
		setPerspective(60.0f, 0.1f, 100.0f);
		update(0);
	}
	
	/**
	 * Updates the camera position and direction according to the car.
	 * @param deltaTime the time since the last update.
	 */
	public void update(float deltaTime) {
		float[] matrix = mCar.getTransformation();
		
		mAngle = (mCar.getRotationY() - 90) * FastMath.DEG_TO_RAD;
		
		mX = FloatMath.sin((FastMath.PI/2 + mAngle)) * RADIUS;
		mZ = FloatMath.cos((FastMath.PI/2 + mAngle)) * RADIUS;
		
		lookAt(matrix[12] - mX, POSITION_Y, matrix[14] - mZ, matrix[12], matrix[13] + 1.5f, matrix[14], 0, 1, 0);
	}
}
